package com.etc.dao.impl;

import java.sql.SQLException;

import javax.sql.rowset.CachedRowSet;

import com.etc.dao.IAdminDao;
import com.etc.entity.Admins;
import com.etc.util.DBUtil;

public class AdminDaoImpl implements IAdminDao {

	/**
	 * 根据管理员姓名和密码查询管理员
	 * adminName:管理员姓名
	 * adminPwd:管理员密码
	 * return:管理员对象
	 */
	@Override
	public Admins queryAdminByName(String adminName,String adminPwd) {
		// TODO Auto-generated method stub
		CachedRowSet crs = DBUtil.executeQuery("select * from admins where adminName=? and adminPwd=?", adminName,adminPwd);
		Admins admin=null;

		try {
			while(crs.next()) {
				int adminId = crs.getInt(1);
				//账号不用读取
				//密码不用读取
				String adminAvatarAddr = crs.getString(4);
				int adminStatus = crs.getInt(5);

				admin = new Admins(adminId, adminName, adminPwd,adminAvatarAddr, adminStatus);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return admin;
	}

	/**
	 * 添加管理员
	 * admin:管理员对象
	 * return:boolean值
	 */
	@Override
	public boolean addAdmin(Admins admin) {
		// TODO Auto-generated method stub

		return DBUtil.executeUpdate("insert into admins values(null,?,?,?,1)",admin.getAdminName(),admin.getAdminPwd(),admin.getAdminAvatarAddr());
	}

	/**
	 * 修改管理员信息
	 * admin:管理员对象
	 * return:boolean值
	 */
	@Override
	public boolean changeAdminInfo(Admins admin) {
		//得到参数中的对象Id
		int adminId = admin.getAdminId();
		String adminName = admin.getAdminName();
		String adminPwd = admin.getAdminPwd();
		String adminAvatarAddr = admin.getAdminAvatarAddr();
		int adminStatus = admin.getAdminStatus();

		return DBUtil.executeUpdate("update admins set adminName=?,adminPwd=?,adminAvatarAddr=?,adminStatus=? where adminId=?", adminName,adminPwd,adminAvatarAddr,adminStatus,adminId);
	}

	@Override
	public Admins queryAdminById(int adminId) {
		CachedRowSet crs = DBUtil.executeQuery("select * from admins where adminId = ?", adminId);
		Admins admin=null;

		try {
			while(crs.next()) {
				int id = crs.getInt(1);
				String adminName = crs.getString(2);
				String adminPwd = crs.getString(3);
				String adminAvatarAddr = crs.getString(4);
				int adminStatus = crs.getInt(5);

				admin = new Admins(id, adminName, adminPwd,adminAvatarAddr, adminStatus);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return admin;
	}

	/**
	 * 修改管理员头像
	 * admin:管理员对象
	 * return:boolean值
	 */
	@Override
	public boolean modifAdminPic(Admins admin) {
		return DBUtil.executeUpdate("update admins set adminAvatarAddr = ? where adminId = ?", admin.getAdminAvatarAddr(),admin.getAdminId());
	}

}
